package com.zjh.flux.controller;

import org.springframework.boot.autoconfigure.security.SecurityProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * @Auther: zjh
 * @Date: 2025/11/4 - 11 - 04 - 22:29
 * @Description: com.zjh.flux.controller
 * @version: 1.0
 */
@RestController
public class HelloController {
    //webflux向下兼容springmvc的配置
    @GetMapping("/hello")
    public String hello(@RequestParam("key") String key) {
        return "hello zjh" + key;
    }

    /**
     * 返回单个数据Mono<String> 、user 、map
     * 返回多个数据Flux<String>
     *
     * @param key
     * @return
     */
    @GetMapping("/momoHello")
    public Mono<String> monoHello(@RequestParam("key") String key) {
        return Mono.just("hello zjh" + key);
    }

    @GetMapping("/fluxHello")
    public Flux<String> fluxHello(@RequestParam("key") String key) {
        return Mono.just("hellozjh" + key).flatMapMany(e -> {
            String[] split = e.split(" ");
            return Flux.fromArray(split);
        });
    }
    @GetMapping("/momoUser")
    public Mono<Integer> momoUser(@RequestParam("key") String key) {
        return Mono.deferContextual(ctx->Mono.just(ctx.get("user")));
    }
}
